<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Compare extends MY_Controller {
// Some code happens here
    public function __construct()
    {
        parent:: __construct();
        $this->load->helper("url");
	    $this->load->model('sanpham');
        $this->load->library('url');    
        $this->load->helper('html');
		$this->load->helper('text');
	    $this->load->library('parser');
	    $this->load->library('session');
        $this->load->library('currency');
    }
	
	public function index()
	{
        $strproduct_id = $this->session->userdata['items'];
        $data['html']='';
        if($strproduct_id!='')
        {
            $arrProducts= explode(',',$strproduct_id);
            $firstProduct = array_slice($arrProducts,0,1);
            $arrInfo = $this->sanpham->get_info_id($firstProduct[0]);
            foreach($arrInfo as $info)
            {
                $product_db['info'][$info->id].= '<td class="infoname">'.$info->Ten.' :</td>';
            }
            foreach($arrProducts as $product){
                $product_db['id'] = $product;
                $product_db['name'].= '<th scope="col">'.$this->sanpham->get_product_name($product).'</th>';
                $product_db['price'].= '<td>'.$this->currency->convert_to_vnd($this->sanpham->get_product_prices($product)).'</td>';
                foreach($arrInfo as $inf)
                {
                    $product_db['info'][$inf->id].= '<td>'.$this->sanpham->get_product_info($product,$inf->id).'</td>';
                }
            }
            
            $data['html'].=$this->parser->parse('compare/main',$product_db,true);
        }
        $this->load->view('test/content',$data);
	}
    public function addproduct($id)
    {
        $product_id = $id;   
        $items='';
        $items = $this->session->userdata('items');
        $arr=explode(',',$items);
        if(count($arr)>=4)
        {
            //$items='';
            //$items.=$id;
            $result = array('kq'=>'false');
            $result = json_encode($result);
        }
        else
        {
            if($items=='')
                $items.=$id;
            else
                $items.=','.$id; 
            
            $arr2=explode(',',$items);
            $result = array('kq'=>'true','name'=>'<li class="com'.$id.'"><a href="javascript:void(0)" onclick="deleteCompare('.$id.')">'.$this->sanpham->get_product_name($id).'</a></li>');
            $result = json_encode($result);
        }
        $this->session->set_userdata('items',$items); 
        echo $result;
        exit;
       // 
    }
    
    public function delete($id)
    {
        $product_id = $id;
        $items='';
        $items = $this->session->userdata('items');
        $items =str_replace($id,'',$items);
        $arr=explode(',',$items);
        foreach($arr as $key=>$temp)
        {
            if($temp=='')
                unset($arr[$key]);
        }
        //var_dump($arr);
        $items=implode(',',$arr);
        
        $this->session->set_userdata('items',$items); 
        $result=$items;
        //var_dump($items);
        echo $result;
        exit;
       // 
    }
 }